package com.example.capsule_chat0322.request;

import android.content.Context;

import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.configuration.HttpConstant;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.utils.AlterUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author zhuzhiming
 * @Date 2022/5/3 19:05
 */
public class RegisterRequest {


    public static void doRegister() {

        Object imei = GlobalAssistMap.getObject("imei");

        if (imei == null) {
            return;
        }

        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/registerByImei/" + imei)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Object context = GlobalAssistMap.getObject("context");
                AlterUtils.alertByToastInSonThread((Context) context, "获取userId失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String userId = response.body().string();
                if (!userId.equals("")) {
                    GlobalAssistMap.putObject("userId", userId);
                    UserInfoBean userInfoBean = UserInfoRequest.doGetUserInfoRequestSync(userId);
                    GlobalAssistMap.putObject("userInfo", userInfoBean);
                }

            }
        });

    }
}
